import { decimalAddition } from '@/utils'

// 将 form 数据转为为一二级树形结构
export const buildTreeData = (feeList) => {
  let stList = []
  feeList.forEach((feeItem) => {
    let feeDetailList = feeItem.feeDetailList
    for (let i = 0; i < feeDetailList.length; i++) {
      let feeCategoryItem = feeDetailList[i]
      let childrenList = feeCategoryItem.childrenList
      let statics = feeCategoryItem.statics
      if (statics) {
        let feeItem = {
          name: feeCategoryItem.feeName,
          randomKey: feeCategoryItem.randomKey,
          children: [],
          feeCategory: feeCategoryItem.feeCategory,
        }
        if (childrenList?.length > 0) {
          // 基础费用需要将里面所有的费项放到一级
          // 其他费用根据放到一二级
          childrenList.forEach((fee) => {
            if (fee.statics) {
              feeItem.children.push({
                name: fee.itemName,
                value: fee.amount || 0,
                costType: fee.costType,
                randomKey: fee.randomKey,
              })
            }
          })
          stList.push(feeItem)
        } else {
          stList.push(feeItem)
        }
      }
    }
  })
  return stList
}
// 除押金外将 form 数据转为为一二级树形结构
export const buildNoDepositFeeTreeData = (feeList) => {
  let stList = []
  feeList.forEach((feeItem) => {
    let feeDetailList = feeItem.feeDetailList
    for (let i = 0; i < feeDetailList.length; i++) {
      let feeCategoryItem = feeDetailList[i]
      if (feeCategoryItem.type !== 'depositFee') {
        let childrenList = feeCategoryItem.childrenList
        let statics = feeCategoryItem.statics
        if (statics) {
          if (childrenList?.length > 0) {
            // 基础费用需要将里面所有的费项放到一级
            // 其他费用根据放到一二级
            let feeItem = {
              name: feeCategoryItem.feeName,
              randomKey: feeCategoryItem.randomKey,
              children: [],
              feeCategory: feeCategoryItem.feeCategory,
            }
            childrenList.forEach((fee) => {
              if (fee.statics) {
                feeItem.children.push({
                  name: fee.itemName,
                  value: fee.amount || 0,
                  costType: fee.costType,
                  randomKey: fee.randomKey,
                })
              }
            })
            stList.push(feeItem)
          }
        }
      }
    }
  })
  return stList
}
// 维持树形数据 childrenToggle 值
const getPrevIsToggle = (randomKey, feeTreeStData) => {
  let feeTreeStDataValue = feeTreeStData.value
  let findResult = feeTreeStDataValue.find((item) => item.randomKey == randomKey)
  if (findResult && findResult.hasOwnProperty('childrenToggle')) {
    return findResult.childrenToggle
  } else {
    return true
  }
}
// 树形结构数据二次处理，对有children的项进行数值汇总，对没有value的项，增加 value 默认值 0
export const processTreeData = (feeList, feeTreeStData) => {
  let tempList = []
  if (feeList && feeList.length > 0) {
    tempList = JSON.parse(JSON.stringify(feeList))
    // 将基础费用拆到一级，并删除基础费用 start
    for (let i = 0; i < tempList.length; i++) {
      let item = tempList[i]
      const feeCategory = item.feeCategory
      if (feeCategory == 0 && item.children?.length > 0) {
        // 删除当前项
        tempList.splice(i, 1)
        let children = item.children
        for (let j = children.length - 1; j >= 0; j--) {
          let childrenItem = children[j]
          tempList.splice(i, 0, childrenItem)
        }
        break
      }
    }
    // 将基础费用拆到一级，并删除基础费用 end
    for (let i = 0; i < tempList.length; i++) {
      let item = tempList[i]
      const { value, children } = item
      // 计算 children 金额汇总
      if (children && children.length > 0) {
        // 处理 childrenToggle
        if (feeTreeStData) {
          item.childrenToggle = getPrevIsToggle(item.randomKey, feeTreeStData)
        }
        let sum = 0
        children.forEach((childItem) => {
          let childValue = childItem.value
          // 付款类型
          if (childItem.costType == 20 && childValue > 0) {
            childItem.value = childValue = -childValue
          }
          if (childValue) {
            sum = decimalAddition(sum, childValue)
          } else {
            sum = decimalAddition(sum, 0)
            childItem.value = 0
          }
        })
        item.value = sum
      } else {
        delete item.children
        delete item.childrenToggle
        if (!value) item.value = 0
      }
    }
  }
  return tempList
}

// 获取费用合计
export const getFeeSum = (orderFee) => {
  let feeVal = buildTreeData(orderFee)
  let feeTreeStData = processTreeData(feeVal)
  let sum = 0
  feeTreeStData.forEach((item) => {
    sum = decimalAddition(sum, item.value)
  })
  return sum
}